package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.common.log.annotation.Log;
import com.atguigu.spzx.common.log.enums.BusinessType;
import com.atguigu.spzx.manager.service.SysRoleService;
import com.atguigu.spzx.model.dto.system.AssginRoleDto;
import com.atguigu.spzx.model.dto.system.SysRoleDto;
import com.atguigu.spzx.model.entity.system.SysRole;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/system/sysRole")
@Tag(name = "角色管理接口")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @GetMapping("/{current}/{size}")
    @Operation(summary = "角色查询方法")
    @Parameters({@Parameter(name="current",description = "当前页",required = true),
                @Parameter(name="size",description = "页大小",required = true),
                @Parameter(name="sysRoleDto",description = "role请求对象",required = false)})
    public Result<PageInfo>findByPage(@PathVariable Integer current,
                                      @PathVariable Integer size,
                                      SysRoleDto sysRoleDto){
        PageInfo<SysRole> pageInfo = sysRoleService.findByPage(current, size, sysRoleDto);
        return   Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "角色管理",businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @Operation(summary = "添加角色方法")
    @Parameters(@Parameter(name = "sysRole",description = "角色信息对象",required = true))
    public Result saveSysRole(@RequestBody SysRole sysRole){
        sysRoleService.addSysRole(sysRole);
        return Result.build(null,ResultCodeEnum.SUCCESS).setMsg("添加角色成功");
    }
    @GetMapping("getById/{id}")
    @Operation(summary = "根据id查询角色方法")
    @Parameters(@Parameter(name = "id",description = "角色id",required = true))
    public Result getById(@PathVariable Long id){
        SysRole sysRole=sysRoleService.getById(id);
        return  Result.build(sysRole,ResultCodeEnum.SUCCESS);
    }

    @PutMapping("updateById")
    @Operation(summary = "修改角色信息方法")
    @Parameters(@Parameter(name = "sysRole",description = "角色信息对象",required = true))
    public Result updateById(@RequestBody SysRole sysRole){
        sysRoleService.updateById(sysRole);
        return Result.build(null,ResultCodeEnum.SUCCESS).setMsg("修改成功");
    }
    @DeleteMapping("deleteById/{id}")
    @Operation(summary = "删除角色方法")
    @Parameters(@Parameter(name = "id",description = "角色id",required = true))
    public Result deleteById(@PathVariable Long id){
        sysRoleService.deleteById(id);
        return Result.build(null,ResultCodeEnum.SUCCESS).setMsg("删除成功");
    }

    @GetMapping("toAssignPage/{userId}")
    @Operation(summary = "获取所有角色以及当前已分配角色id")
    @Parameters(@Parameter(name = "userId",description = "用户id",required = true))
    public Result<Map<String,Object>> toAssignPage(@PathVariable Long userId){
       Map<String,Object> map= sysRoleService.toAssignPage(userId);
       return Result.build(map,ResultCodeEnum.SUCCESS);
    }
    @PostMapping("toAssginRole")
    @Operation(summary = "修改分配用户角色信息")
    public Result toAssginRole(@RequestBody AssginRoleDto assginRoleDto){
        sysRoleService.toAssginRole(assginRoleDto);
        return  Result.build(null,ResultCodeEnum.SUCCESS).setMsg("修改成功");
    }
}
